package com.bjh.server;

import cn.hutool.json.JSONUtil;
import com.bjh.model.Message;
import com.bjh.model.MessageBody;
import com.bjh.model.MessageHead;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class MyServerHandler extends SimpleChannelInboundHandler<Message> {


    private final static Logger logger = LoggerFactory.getLogger(MyServerHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message message) throws Exception {
        System.out.println("这是客户端发送的消息" + JSONUtil.toJsonPrettyStr(message));

        Message messageResponse = new Message();
        MessageHead messageHead = new MessageHead();
        messageHead.setCommond(9988);
        messageHead.setMessageType(0x0);
        messageHead.setClientType(1);
        messageHead.setVersion(2);
        messageHead.setAppId(3);

        String msg = "这是服务端发送给你的消息";
        messageHead.setBodyLen(msg.getBytes().length);
        String imei = "12-euri-1234";
        messageHead.setImeiLen(imei.getBytes().length);

        MessageBody messageBody = new MessageBody();

        messageBody.setImei(imei);
        messageBody.setData(msg);

        messageResponse.setMessageHead(messageHead);
        messageResponse.setMessageBody(messageBody);

        String repsMsg = "hello";

        ctx.writeAndFlush(repsMsg);
    }
}
